<script>

  chrome.extension.onRequest.addListener(function (request, sender) {

	//alert("Whote "+request.source);
    if (request.source == "On"){
       chrome.tabs.getAllInWindow(null, function(tabs){
          tabs.forEach(function(tab){
             localStorage["icon"] = "ic_media_repeat_on.png";          
    	     chrome.pageAction.setIcon({"tabId": tab.id, "path":localStorage["icon"]});
	     chrome.pageAction.show(tab.id);
  	  });
       });
    }else if (request.source == "Single"){
       chrome.tabs.getAllInWindow(null, function(tabs){
          tabs.forEach(function(tab){
             localStorage["icon"] = "ic_media_repeat_one.png";          
    	     chrome.pageAction.setIcon({"tabId": tab.id, "path":localStorage["icon"]});
	     chrome.pageAction.show(tab.id);
  	  });
       });  
     }else if (request.source == "Off"){
       chrome.tabs.getAllInWindow(null, function(tabs){
          tabs.forEach(function(tab){
             localStorage["icon"] = "ic_media_repeat_off.png";          
    	     chrome.pageAction.setIcon({"tabId": tab.id, "path":localStorage["icon"]});
	     chrome.pageAction.show(tab.id);
  	  });
       });  
     }
  });


  function FindMusicBetaTab(callback) {
    chrome.windows.getAll({populate: true}, function(windows) {
        var pattern = 'https?\:\/\/music\.google\.com\/music\/listen.*';
        for (var window = 0; window < windows.length; window++) {
          for (var i = 0; i < windows[window].tabs.length; i++) {
            if (windows[window].tabs[i].url.match(pattern)) {
              callback(windows[window].tabs[i].id);
              return;
            }
          }
        }
        callback(null);
      });  
  }
  
  


    function setIconNow(tabId, changeInfo, tab) {
	localStorage["icon"] = "icon.png";
	chrome.pageAction.setIcon({"tabId": tabId, "path":localStorage["icon"]});
	chrome.pageAction.show(tabId);
	    FindMusicBetaTab(function(tab_id) {
	        if (tab_id){
	           chrome.tabs.sendRequest(tab_id, {tere: 'vajutus'});
	        }
	        else{
	           chrome.tabs.getAllInWindow(null, function(tabs){
	              tabs.forEach(function(tab){
	                 localStorage["icon"] = "ic_media_repeat_off.png";          
	    	         chrome.pageAction.setIcon({"tabId": tab.id, "path":localStorage["icon"]});
		         chrome.pageAction.show(tab.id);
	  	      });
                   }); 
	        }
	    });	
    }
    
    
  function sendCommand(command) {
    FindMusicBetaTab(function(tab_id) {
        if (tab_id) {
          if (command == "foreground") {
            chrome.tabs.update(tab_id, {selected: true});
          } else {
            chrome.tabs.executeScript(tab_id,{
                  code: "location.assign('javascript:SJBpost(\"" + command +
                        "\");void 0');",
                  allFrames: true
                });
          }
        } else {
          chrome.tabs.create({url: 'http://music.google.com/music/listen',
                              selected: true});
        }
      });
  }    


   chrome.pageAction.onClicked.addListener(function (tab) {
     sendCommand("toggleRepeat");
     chrome.tabs.sendRequest(tab.id, {tere: 'vajutus'});
   });
   
   
   chrome.extension.onRequest.addListener(function(request, sender, callback) {
      
   });
   

  chrome.tabs.onAttached.addListener(setIconNow);
  chrome.tabs.onDetached.addListener(setIconNow);
  chrome.tabs.onCreated.addListener(setIconNow);
  chrome.tabs.onRemoved.addListener(setIconNow);
  chrome.tabs.onUpdated.addListener(setIconNow);

</script>